<?php
	include("gen.php");
	$cmd = get_datan("cmd");
	switch($cmd){
		//Get a single OPD Record from the ID
		case 1:
			get_opd_record();
			break;

		case 2:
			get_opd_records();
			break;

		case 3:
			update_opd_record();
			break;

		case 4:
			delete_opd_record();
			break;

		case 5:
			create_opd_record();
			break;

		default:
			echo "{";
			echo jsonn("result", 0). ",";
			echo jsons("message", "unknown command");
			echo "}";
	}


	function get_opd_record(){
		include_once("opdClass.php");

		$id = get_datan("id");

		$o = new opdClass();
		$x = new opdClass();
		$y = new opdClass();

		$row = $o->get_opd_record($id);
		

		if(!$row){
			echo "{";
			echo jsonn("result", 0).",";
			echo jsons("message", "No OPD Record Found!");
			echo "}";
			return;
		}

		$row = $o->fetch();
		
		$x->get_community_member($row["community_member_id"]);
		$x = $x->fetch();

		$y->get_cho($row["idcho"]);
		$y = $y->fetch();

		echo "{";
		echo jsonn("result", 1) . ",";
		echo '"opd_record":{';
		echo jsonn("record_id", $id) . ",";
		echo jsonn("community_member_id", $row["community_member_id"]) . ",";
		echo jsons("community_member_name", $x["fullname"]) . ",";
		echo jsonn("opd_case_id", $row["opd_case_id"]) . ",";
		echo jsons("record_type", $row["record_type"]) . ",";
		echo jsonn("idcho", $row["idcho"]) . ",";
		echo jsons("cho_name", $y["cho_name"]) . ",";
		echo jsons("note", $row["note"]);
		echo "}";
		echo "}";
	}

	function get_opd_records(){
		include_once("opdClass.php");

		$o = new opdClass();

		$row = $o->get_opd_records();

		if(!$row){
			echo "{";
			echo jsonn("result", 0) . ",";
			echo jsons("message", "No OPD Records Found!");
			echo "}";
			return;
		}

		$row = $o->fetch();

		echo "{";
		echo jsonn ("result", 2) . ",";
		echo '"opd_records:":[';

		while($row){

			echo "{";
			//opd record id
			echo jsonn("opd_record_id", $row["opd_record_id"]) . ",";
			//community member id
			echo jsonn("community_member_id", $row["community_member_id"]) . ",";
			//opd case id
			echo jsonn("opd_case_id", $row["opd_case_id"]) . ",";
			//record type
			echo jsons("record_type", $row["record_type"]) . ",";
			//idcho
			echo jsonn("idcho", $row["idcho"]) . ",";
			//note
			echo jsons("note", $row["note"]);
			echo "}";
		}

		echo "]}";
	}


	function update_opd_record(){
		include_once("opdClass.php");

		$o = new opdClass();

		$id = get_datan("id");
		$community_member_id = get_datan("cmid");
		$opd_case_id = get_datan("ocid");
		$record_type = get_data("rt");
		$idcho = get_datan("idc");
		$n = get_data("n");

		if(!$id){
			//Error message
			echo '{"result":0, "message":"Updating OPD Record Failed"}';
			return;
		}

		if(!$o->update_opd_record($id, $community_member_id, $opd_case_id, $record_type, $idcho, $n)){
			//Error message
			echo '{"result":"0, "message":"Update Failed!"}';
			return;
		}

		echo "{";
		echo jsonn("result", 3) . ",";
		echo '"opd_record":{';
		echo jsonn("opd_record_id", $id) . ",";
		echo jsonn("community_member_id", $community_member_id) . ",";
		echo jsonn("opd_case_id", $opd_case_id) . ",";
		echo jsons("record_type", $record_type) . ",";
		echo jsonn("idcho", $idcho) . ",";
		echo jsons("note", $note);
		echo "}";
		echo "}";
	}

	function delete_opd_record(){
		include_once("opdClass.php");

		$o = new opdClass();

		$id = get_datan("id");

		if(!$id){
			//Error Message
			echo '{"result": 0 "message":"Failed to Delete OPD Record"}';
			return;
		}

		$o->delete_opd_record($id);

		echo "{";
		echo jsonn("result", 4) . ",";
		echo jsons("message", "Delete Successful!");
		echo "}";
	}

	function create_opd_record(){
		include_once("opdClass.php");

		$o = new opdClass();

		$community_member_id = get_datan("cmid");
		$opd_case_id = get_datan("ocid");
		$record_type = get_data("rt");
		$idcho = get_datan("idc");
		$n = get_data("n");

		if(!$o->insert_opd_record($community_member_id, $opd_case_id, $record_type, $idcho, $n)){
			echo '{"result":"0, "message":"Insert Failed!"}';
			return;
		}

		echo "{";
		echo jsonn("result", 5) . ",";
		echo '"opd_record":{';
		echo jsonn("community_member_id", $community_member_id) . ",";
		echo jsonn("opd_case_id", $opd_case_id) . ",";
		echo jsons("record_type", $record_type) . ",";
		echo jsonn("idcho", $idcho) . ",";
		echo jsons("note", $n);
		echo "}";
		echo "}";

	}
?>